package com.bawei.arouter

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.cache.CacheManager
import com.bawei.cache.CacheStrategy

/**
 * 登录状态校验拦截器
 */
@Interceptor(priority = 5, name = "登录状态校验拦截器")
class LoginStatusInterceptor: IInterceptor {

    private var mContext: Context? = null

    //配置需要拦截页面路由Path
    private val checkPaths = arrayOf(
        "/send/video",
    )

    override fun init(context: Context?) {
        mContext = context
    }

    /**
     * 拦截操作
     */
    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        if (checkPaths.contains(postcard?.path)) {
            checkLoginToken(postcard, callback)
        } else {
            callback?.onContinue(postcard)
        }
    }

    /**
     * 检查登录Token
     */
    private fun checkLoginToken(postcard: Postcard?, callback: InterceptorCallback?){
        var token =  CacheManager.getCache(CacheStrategy.MMKV).getString("token")
        if (token.isNullOrEmpty()) {
            ARouter.getInstance().build("/login/activity").navigation()
        } else {
            callback?.onContinue(postcard)
        }
    }


}